using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


namespace Pampa.Grid.UI.CustomControls
{
    public class WindowPager : AbstractPager
    {
        private int windowSize;

        public WindowPager()
        {
            windowSize = 4;
        }
        
        public int WindowSize
        {
            get { return windowSize; }
            set { windowSize = value; }
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            CustomGrid parentList = CustomGrid.LoadFromSession(this.GridID);

            writer.AddAttribute("id", this.ID);
            writer.AddAttribute("class", this.CssClass);
            writer.AddStyleAttribute("height", "25px");
            writer.AddStyleAttribute("width", this.Width.ToString());
            writer.AddStyleAttribute("display", this.Show ? "" : "none");

            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            writer.WriteLine("<table>");
            writer.WriteLine("<tr>");

            writer.WriteLine("<td align=\"center\">");
            writer.WriteLine("<a href=\"javascript:ClientPager_movePrevious('" + this.ID + "')\"> < </a>");

            writer.WriteLine("<span id=\"counter_" + this.ID + "\">");

            int pageFrom = System.Math.Max(1, parentList.PageNumber - this.WindowSize);
            int pageTo = System.Math.Min(parentList.LastPage, parentList.PageNumber + this.WindowSize);
            string className;

            for (int page = pageFrom; page <= pageTo; page++)
            {
                if (page == parentList.PageNumber)
                    className = "selectedPage";
                else 
                    className = "unselectedPage";

                writer.WriteLine("<a class=\"" + className + "\" href=\"javascript:ClientPager_moveTo('" + this.ID + "', " + page.ToString() + ")\">" + page.ToString() + "</a>");
            }

            writer.WriteLine("</span>");

            writer.WriteLine("<a href=\"javascript:ClientPager_moveNext('" + this.ID + "')\"> > </a>");
            writer.WriteLine("</td>");

            writer.WriteLine("</tr>");
            writer.WriteLine("</table>");

            writer.RenderEndTag();
        }

        protected override void RenderProperties(HtmlTextWriter writer)
        {
            base.RenderProperties(writer);

            writer.WriteLine("obj" + this.ID + ".refresh = WindowPager_refresh;");
            writer.WriteLine("obj" + this.ID + ".windowSize = " + this.WindowSize.ToString() + ";");
        }
    }
}
